package com.test.design.material.materialdesign;

import android.animation.Animator;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.ItemTouchHelper;

import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;

import view.MyRecyclerView;

public class ScrollingActivity extends AppCompatActivity implements OnStartDragSwipeInterface,View.OnTouchListener{
    MyRecyclerView myRecyclerView;
    private ItemTouchHelper itemTouchHelper;
    private CoordinatorLayout coordinatorLayout;
    private ItemTouchHelperAdapterInterface itemTouchHelperAdapterInterface;
    private MessageItemTouchCallback messageItemTouchCallback;
    private MyRecyclerAdapter myRecyclerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("测试测试");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinatorlayout);
        coordinatorLayout.setOnTouchListener(this);

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
//
//
//            int[] location = new int[2];
//            view.getLocationOnScreen(location);
//            CircularAnimView.fullActivity(ScrollingActivity.this,
//                    new Point(location[0]+view.getWidth()/2,location[1]+view.getHeight()/2))
//                    .colorOrImageRes(R.color.colorPrimary)  //注释掉，因为该颜色已经在App.class 里配置为默认色
//                    .deployReturnAnimator(new CircularAnimView.OnAnimatorDeployListener() {
//                @Override
//                public void deployAnimator(Animator animator) {
//                        //this .setDuration() with override CircularAnimView.setDuration().
//                        animator.setDuration(500);
//                        animator.setInterpolator(new AccelerateInterpolator());
//                }
//            }).go(new CircularAnimView.OnAnimationEndListener() {
//                @Override
//                public void onAnimationEnd() {
//                    startActivity(new Intent(ScrollingActivity.this, ImageActivity.class));
//                    overridePendingTransition(0,0);
//                }
//            });
//
//
//
//            }
//        });

        List<String> dataList = new ArrayList<>(2);
        for(int i= 0;i<50;i++){
            dataList.add("测试测试列表"+i);
        }

        myRecyclerView = (MyRecyclerView)findViewById(R.id.recyclerview);
        myRecyclerAdapter = new MyRecyclerAdapter(this,dataList,this);
        myRecyclerView.setAdapter(myRecyclerAdapter);


        messageItemTouchCallback = new MessageItemTouchCallback(myRecyclerAdapter);


        itemTouchHelper = new ItemTouchHelper(messageItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(myRecyclerView);
        myRecyclerAdapter.setOnItemClickListener(new MyRecyclerAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(String s) {
                int i = Integer.parseInt(s);
                MyRecyclerAdapter.ViewHolder viewHolder =
                        (MyRecyclerAdapter.ViewHolder )myRecyclerView.findViewHolderForAdapterPosition(i);
                viewHolder.red_point.dismissView();
                ImageView imageView = viewHolder.item_recycler_img;
                Intent intent = new Intent(ScrollingActivity.this,ImageActivity.class);
                Bundle bundle = ActivityOptions.makeSceneTransitionAnimation(
                        ScrollingActivity.this,imageView,imageView.getTransitionName()).toBundle();
                startActivity(intent,bundle);


            }
        });

    }
    @Override
    public void onStartDragSwipe(MyRecyclerAdapter.ViewHolder viewHolder){
        //主动触发滑动
        itemTouchHelper.startDrag(viewHolder);
//        itemTouchHelper.startrSwipe(viewHolder);
    }
    // 拖拽执行
    @Override
    public void doDragRedPoint(int position) {
        Snackbar.make(coordinatorLayout, "Replace with your own action:"+position,
                Snackbar.LENGTH_LONG).setAction("Action", null).show();
//        myRecyclerAdapter.notifyItemChanged(position,position);
//        myRecyclerAdapter.notifyItemRemoved(position);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scrolling, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            /**
             * 点击的开始位置
             */
            case MotionEvent.ACTION_DOWN:
                int x = (int)event.getX();
                int y = (int)event.getY();
                Point point = new Point(x,y);
                CircularAnimView.fullActivity(ScrollingActivity.this, point)
                        .colorOrImageRes(R.color.colorPrimary)  //注释掉，因为该颜色已经在App.class 里配置为默认色
                        .deployReturnAnimator(new CircularAnimView.OnAnimatorDeployListener() {
                            @Override
                            public void deployAnimator(Animator animator) {
                                //this .setDuration() with override CircularAnimView.setDuration().
                                animator.setDuration(500);
                                animator.setInterpolator(new AccelerateInterpolator());
                            }
                        }).go(new CircularAnimView.OnAnimationEndListener() {
                    @Override
                    public void onAnimationEnd() {
                        startActivity(new Intent(ScrollingActivity.this, ImageActivity.class));
                        overridePendingTransition(0,0);
                    }
                });
                break;
            /**
             * 触屏实时位置
             */
            case MotionEvent.ACTION_MOVE:
                messageItemTouchCallback.
                        getItemTouchHelperAdapterInterface().runLayoutAnimation(myRecyclerView);

                break;
            /**
             * 离开屏幕的位置
             */
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        /**
         *  注意返回值
         *  true：view继续响应Touch操作；
         *  false：view不再响应Touch操作，故此处若为false，只能显示起始位置，不能显示实时位置和结束位置
         */
        return true;
    }
}
